
import flash.display.Sprite;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import haxe.FastList;

import flash.display.Shape;

//import flash.utils.Timer;
import haxe.Timer;
//import flash.utils.Timer;
//import flash.events.TimerEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import CircleModel;
import CircleView;
import HelperMethods;

import UIController;

import EnemyShipController;
import BossShipController;

import CircleMV;
class BossEnemyInfo
{
public var BossEnemyPoint:Point;
public var BossEnemyType:String;
public var BossEnemyColor:Int;
public function new(inPoint:Point,inType:String,inColor:Int)
  {
    BossEnemyPoint = inPoint;
    BossEnemyType = inType;
    BossEnemyColor = inColor;
  }
}

class Scene extends Sprite{
    //Circle
  private var  _c1:ShipModel;
  private var _UIController:ShipController; 
  private var _EnemyController:EnemyShipController; 
  private var _c1_View:ShipView;
  private var _UIView:UIView;
  public var running:Bool;
  public var numOfFrames:Int;
  public var TotalMoney:Int;
  private var PowerUps:Int;
  private var MoneyMultiplier:Float;
  public var FadeRate:Int;
  public var ShotsFiredRate:Int;
  
  //Particle
  private var _circles:FastList<CircleMV>;
  private var _enemyCircles:FastList<CircleMV>;
  public var BossController:BossShipController;
  //private var _views:FastList<CircleView>;
  private var _c2:CircleModel;
  private var _c2_View:CircleView;
  private var _health:HealthBarView;
  private var _finishScreen:Sprite;
  private var _finished:TextField;
  private var _moneyCollected:TextField;
  private var _shotsOnScreen:TextField;
  private var _framesLasted:TextField;
  private var _totalAmount:TextField;
  private var _continueButton:Sprite;
  //Status box
  private var _statusBox:TextField;

  private static inline var InvincibleEnemyColor:Int = 0xFFFFFF;
  private static inline var EnemyColor:Int = 0xFF0000;
  private static inline var ShootingEnemyColor:Int = 0xFFFF00;
  private static inline var EnemyShootingEnemyColor:Int = 0xFF7722;
  public var State:String;
  private var StartBoss:Int;
  private var BossDefeated:Int;
  private var Difficulty:Int;
  private var waitedFrames:Int;
  private var _collision:Bool;

  private var mKeyDown:Array<Bool>;
  private var Pause:String;
  public var pauseMenu:PauseMenu;


    private var  myTimer:Timer;
    private static inline var FRAME_RATE:Int = 40;
    private var _period:Float;
    private var _beforeTime:Int;
    private var _afterTime:Int;
    private var _timeDiff:Int;
    private var _sleepTime:Int;
    private var _overSleepTime:Int;
    private var _excess:Int;
   
  
  public function new(shipSize:Int,shipHealth:Int,PowerUpAppearance:Int,
		      InvincibilityLength:Int,PowerUpFade:Int,inMoneyMultiplier:Float,
		      inShotsFiredRate:Int,inDifficulty:Int)
  {  
    super();
    running = false;
    State = "Playing";
    Pause = "UnPaused";
    Difficulty = inDifficulty;
    numOfFrames = 0;
    PowerUps = PowerUpAppearance;
    MoneyMultiplier = inMoneyMultiplier;
    ShotsFiredRate = inShotsFiredRate;
    FadeRate = PowerUpFade;
    flash.Lib.current.addChild(this);
    _collision = false;
    //Circle
    _c1 = new ShipModel("Player",shipHealth,InvincibilityLength, shipSize,0x000000);

    _UIController = new ShipController(_c1);
    _c1_View = new ShipView(_c1, _UIController, stage);
      _health = new HealthBarView(_c1);
        addChild(_health);
    //_UIView = new UIView(_c1, _UIController, stage);

    _c1.setX = 300;
    _c1.setY = 200;

    //Circle2
  _circles = new FastList<CircleMV>();
  _enemyCircles = new FastList<CircleMV>();
  
    //Add the status box
    _statusBox = new TextField();
    _statusBox.width =200;
    addChild(_statusBox);
    
     //myTimer = new Timer(35);
     //myTimer.addEventListener("timer", OnEnter);
this.addEventListener(Event.ENTER_FRAME, OnEnter);
     _finishScreen = new Sprite();
     var shape:Shape = new Shape();
     shape.graphics.lineStyle(1);
     shape.graphics.beginFill(0xFFFFFF);
     shape.graphics.drawCircle(0, 0, 125);
     shape.graphics.endFill();
     shape.x = 300;
     shape.y = 150;
     _finishScreen.addChild(shape);

     _finished = new TextField();
     _finished.x = 225;
     _finished.y = 80;
     _finished.htmlText = "<u>Finished</u>";
     _finished.width = 150;
     _finished.autoSize=TextFieldAutoSize.CENTER;
     _finishScreen.addChild(_finished);


     _moneyCollected = new TextField();
     _moneyCollected.x = 225;
     _moneyCollected.y = 100;
     _moneyCollected.text ="";
     _moneyCollected.width = 150;
     _moneyCollected.autoSize=TextFieldAutoSize.LEFT;
     _finishScreen.addChild(_moneyCollected);

     _shotsOnScreen = new TextField();
     _shotsOnScreen.x = 225;
     _shotsOnScreen.y = 120;
     _shotsOnScreen.text ="";
     _shotsOnScreen.width = 150;
     _shotsOnScreen.autoSize=TextFieldAutoSize.LEFT;
     _finishScreen.addChild(_shotsOnScreen);

     _framesLasted = new TextField();
     _framesLasted.x = 225;
     _framesLasted.y = 140;
     _framesLasted.text ="";
     _framesLasted.width = 150;
     _framesLasted.autoSize=TextFieldAutoSize.LEFT;
     _finishScreen.addChild(_framesLasted);

     _totalAmount = new TextField();
     _totalAmount.x = 225;
     _totalAmount.y = 160;
     _totalAmount.text ="";
     _totalAmount.width = 150;
     _totalAmount.wordWrap = true;
     _totalAmount.autoSize=TextFieldAutoSize.LEFT;
     _finishScreen.addChild(_totalAmount);


     _continueButton = new Sprite();
     shape = new Shape();
     shape.graphics.lineStyle(1);
     shape.graphics.beginFill(0xFFFFFF);
     shape.graphics.drawCircle(0, 0, 25);
     shape.graphics.endFill();
     var continueText = new TextField();
     if(Difficulty >= 1)
     {
      continueText.text = "Continue";
     }
     else
     {
      continueText.text = "    Quit";
     }
     continueText.autoSize = TextFieldAutoSize.LEFT;
     _continueButton.addChild(shape);
     _continueButton.addChild(continueText);
     continueText.x = -25;
     continueText.y = -10;
     continueText.selectable = false;
     continueText.mouseEnabled = false;
     _continueButton.x = 300;
     _continueButton.y = 250;
     _continueButton.buttonMode = true;
     _finishScreen.addChild(_continueButton);
     _continueButton.addEventListener(MouseEvent.CLICK, onContinueButtonClick);
     
     //Load();
//      addBoss();
     mKeyDown = [];
    pauseMenu = new PauseMenu();
    pauseMenu.addEventListener("Quit",onQuit);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown );
    stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp );
  
  }

public function OnKeyUp (event:KeyboardEvent)
    {
      mKeyDown[event.keyCode] = false;
      //lastUp = event.keyCode;
    }

public function OnKeyDown(event:KeyboardEvent)
   {
      // When a key is held down, multiple KeyDown events are generated.
      // This check means we only pick up the first one.
      if (!mKeyDown[event.keyCode])
      {
         // Store for use in game
         mKeyDown[event.keyCode] = true;
        if(State == "Playing")
        {
	  //I do this here, because if they hold down P,
	 // it won't pause and unpause constantly.
	  if(mKeyDown[ 80 ] == true)
	  {
	      if(Pause == "Paused")
                {
                  Pause = "UnPaused";
		  removeChild(pauseMenu);
                }
              else 
                {
                  Pause = "Paused";
		  addChild(pauseMenu);
                }
	  }
	}
      }
  }

  public function Load()
  {
     addChild(_c1_View); 
     //myTimer.start();
     running = true;
     numOfFrames = 000;
     waitedFrames = 0;
     if(Difficulty <= 1)
     {
	StartBoss = 5000;
     }
     if(Difficulty == 2)
     {
	StartBoss = 6000;
     }
     if(Difficulty >= 3)
     {
	StartBoss = 7000;
     }
     BossDefeated = 0;
  }

  public function onContinueButtonClick(event:MouseEvent)
  {
    
    removeChild(_finishScreen);
    UnLoad();
    if(Difficulty >= 1)
    {
    dispatchEvent(new Event("SceneFinished"));
    }
    else
    {
    State = "Quit";
    dispatchEvent(new Event("SceneQuit"));
    }
  }

  public function onQuit(event:Event)
  {
    State = "Quit";
    removeChild(pauseMenu);
    UnLoad();
    dispatchEvent(new Event("SceneQuit"));
  }

  public function UnLoad()
  {
    
    for(circle in _circles.iterator())
    {
      removeChild(circle.view);
      circle.model = null;
      _circles.remove(circle);
    }
    if(BossController != null)
    {
      for(circle in BossController.subModels.iterator())
      {
	removeChild(circle.view);
	circle.model = null;

	_enemyCircles.remove(circle);
      }
    }
    for(circle in _enemyCircles.iterator())
    {

      removeChild(circle.view);
      circle.model = null;

      _enemyCircles.remove(circle);
    }
    removeChild(_c1_View);
    removeChild(_health);
    if(this.contains(_statusBox))
    {
      removeChild(_statusBox);
    }
    //myTimer.stop();
    running = false;
    this.removeEventListener(Event.ENTER_FRAME, OnEnter);
  }

  private function addShot()
  {
    _c2 = new CircleModel("Shot",3);
    _c2_View = new CircleView(_c2);

    addChild(_c2_View);
    var random = Math.random()*2*Math.PI;
    _c2.setX = Math.ceil( _c1.xPos + (_c1.radius + _c2.radius + 1)* Math.cos(random));
    _c2.setY = Math.ceil(_c1.yPos + (_c1.radius + _c2.radius + 1) * Math.sin(random));
    _c2.vx = (1)* Math.cos(random);
    _c2.vy = (1) * Math.sin(random);
    _c2.friction = .999;
    _circles.add(new CircleMV(_c2,_c2_View));
    _c2 = null;
    _c2_View = null;
  }

  private function addEnemyShootingEnemy()
  {
    var tempCircle = new CircleModel("EnemyShootingEnemy",3,0xFF7722);
    _EnemyController = new EnemyShipController(tempCircle,_c1);
    var tempCircle_View = new CircleView(tempCircle);

    addChild(tempCircle_View);
    var random = Math.random()*2*Math.PI;
    tempCircle.setX = Math.ceil( _c1.xPos + (_c1.radius + tempCircle.radius + 250)* Math.cos(random));
    tempCircle.setY = Math.ceil(_c1.yPos + (_c1.radius + tempCircle.radius + 150) * Math.sin(random));
    _enemyCircles.add(new CircleMV(tempCircle,tempCircle_View));
  }

  private function addShootingEnemy()
  {
    var tempCircle = new CircleModel("ShootingEnemy",3,0xFFFF00);
    _EnemyController = new EnemyShipController(tempCircle,_c1);
    var tempCircle_View = new CircleView(tempCircle);

    addChild(tempCircle_View);
    var random = Math.random()*2*Math.PI;
    tempCircle.setX = Math.ceil( _c1.xPos + (_c1.radius + tempCircle.radius + 250)* Math.cos(random));
    tempCircle.setY = Math.ceil(_c1.yPos + (_c1.radius + tempCircle.radius + 150) * Math.sin(random));
    _enemyCircles.add(new CircleMV(tempCircle,tempCircle_View));
  }

  private function addInvincibleEnemy()
  {
    var tempCircle = new CircleModel("InvincibleEnemy",3,InvincibleEnemyColor);
    _EnemyController = new EnemyShipController(tempCircle,_c1);
    var tempCircle_View = new CircleView(tempCircle);

    addChild(tempCircle_View);
    var random = Math.random()*2*Math.PI;
    tempCircle.setX = Math.ceil( _c1.xPos + (_c1.radius + tempCircle.radius + 250)* Math.cos(random));
    tempCircle.setY = Math.ceil(_c1.yPos + (_c1.radius + tempCircle.radius + 150) * Math.sin(random));
    _enemyCircles.add(new CircleMV(tempCircle,tempCircle_View));
  }

  private function addEnemy()
  {
    var tempCircle = new CircleModel("Enemy",3,0xFF0000);
    _EnemyController = new EnemyShipController(tempCircle,_c1);
    var tempCircle_View = new CircleView(tempCircle);

    addChild(tempCircle_View);
    var random = Math.random()*2*Math.PI;
    tempCircle.setX = Math.ceil( _c1.xPos + (_c1.radius + tempCircle.radius + 250)* Math.cos(random));
    tempCircle.setY = Math.ceil(_c1.yPos + (_c1.radius + tempCircle.radius + 150) * Math.sin(random));
    _enemyCircles.add(new CircleMV(tempCircle,tempCircle_View));
  }

  private function addBoss()
  {
    
    var  Boss_Points:FastList<BossEnemyInfo> = new FastList<BossEnemyInfo>();



//     Boss_Points.add(new BossEnemyInfo(new Point(2,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(18,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(22,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(26,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,20),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(62,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(66,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(70,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,20),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,20),"Enemy",EnemyColor));



//     Boss_Points.add(new BossEnemyInfo(new Point(2,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(18,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(22,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,25),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(66,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(70,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,25),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,25),"Enemy",EnemyColor));




//     Boss_Points.add(new BossEnemyInfo(new Point(2,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(18,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(22,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,30),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,30),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,30),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,30),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,30),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(66,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(70,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,30),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,30),"Enemy",EnemyColor));

//     Boss_Points.add(new BossEnemyInfo(new Point(2,35),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,35),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,35),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(18,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(22,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,35),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,35),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,35),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,35),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(66,35),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(70,35),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,35),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,35),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,35),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,35),"Enemy",EnemyColor));

//     Boss_Points.add(new BossEnemyInfo(new Point(2,40),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,40),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(14,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(18,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(22,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,40),"EnemyShootingEnemy",EnemyShootingEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,40),"EnemyShootingEnemy",EnemyShootingEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(66,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(70,40),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(74,40),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,40),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,40),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,40),"Enemy",EnemyColor));

//     Boss_Points.add(new BossEnemyInfo(new Point(2,45),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,45),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(14,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(18,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(22,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,45),"EnemyShootingEnemy",EnemyShootingEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,45),"EnemyShootingEnemy",EnemyShootingEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(66,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(70,45),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(74,45),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,45),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,45),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,45),"Enemy",EnemyColor));
// 

//     Boss_Points.add(new BossEnemyInfo(new Point(6,50),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(14,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(18,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(22,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(66,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(70,50),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(74,50),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,50),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,50),"Enemy",EnemyColor));


//     Boss_Points.add(new BossEnemyInfo(new Point(2,55),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,55),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,55),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,55),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(18,55),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(22,55),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,55),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(66,55),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(70,55),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,55),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,55),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,55),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,55),"Enemy",EnemyColor));

//     Boss_Points.add(new BossEnemyInfo(new Point(2,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(18,60),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(22,60),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,60),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,60),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,60),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(66,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(70,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,60),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,60),"Enemy",EnemyColor));

//     Boss_Points.add(new BossEnemyInfo(new Point(2,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(18,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(22,65),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(26,65),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,65),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,65),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,65),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,65),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,65),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,65),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,65),"InvincibleEnemy",InvincibleEnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,65),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(62,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(66,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(70,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,65),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,65),"Enemy",EnemyColor));

//     Boss_Points.add(new BossEnemyInfo(new Point(2,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(6,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(10,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(14,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(18,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(22,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(26,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(30,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(34,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(38,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(42,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(46,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(50,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(54,70),"Enemy",EnemyColor));
    Boss_Points.add(new BossEnemyInfo(new Point(58,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(62,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(66,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(70,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(74,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(78,,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(82,,70),"Enemy",EnemyColor));
//     Boss_Points.add(new BossEnemyInfo(new Point(86,,70),"Enemy",EnemyColor));





    BossController = new BossShipController(_c1);
    for(Boss_Point in Boss_Points.iterator())
    {
      var tempCircle = new CircleModel(Boss_Point.BossEnemyType,3,Boss_Point.BossEnemyColor);
      var tempCircle_View = new CircleView(tempCircle);
      BossController.subModels.add(new CircleMV(tempCircle,tempCircle_View));

      addChild(tempCircle_View);
      var random = Math.random()*2*Math.PI;
      tempCircle.setX = Boss_Point.BossEnemyPoint.x;
      tempCircle.setY = Boss_Point.BossEnemyPoint.y;
      //_enemyCircles.add(new CircleMV(tempCircle,tempCircle_View));
    }
  }
public function randomCirclePosition():Float
{
  return Math.random()*2*Math.PI;
}

 private function addMoney(xPos:Float,yPos:Float)
  {
    _c2 = new CircleModel("Money",3,0x00FF00,FadeRate);
    _c2_View = new CircleView(_c2);
    _c2.Fading = 1;

    addChild(_c2_View);
    
    _c2.setX = xPos;
    _c2.setY = yPos;
    _c2.vx = 0;
    _c2.vy = 0;
    _c2.friction = .999;
    _circles.add(new CircleMV(_c2,_c2_View));
  }

 private function addHealth(xPos:Float,yPos:Float)
  {
    _c2 = new CircleModel("Health",3,0x0000FF,FadeRate);
    _c2.Fading = 1;
    _c2_View = new CircleView(_c2);

    addChild(_c2_View);
    
    _c2.setX = xPos;
    _c2.setY = yPos;
    _c2.vx = 0;
    _c2.vy = 0;
    _c2.friction = .999;
    _circles.add(new CircleMV(_c2,_c2_View));
  }

 private function addInvincibility(xPos:Float,yPos:Float)
  {
    _c2 = new CircleModel("Invincibility",3,0xFF00FF,FadeRate);
    _c2_View = new CircleView(_c2);
    _c2.Fading = 1;

    addChild(_c2_View);
    
    _c2.setX = xPos;
    _c2.setY = yPos;
    _c2.vx = 0;
    _c2.vy = 0;
    _c2.friction = .999;
    _circles.add(new CircleMV(_c2,_c2_View));
    
  }

  private function LevelOver()
  {
    //Load sum menu.
    if(State == "Playing")
    {
      var TotalShots = 0;
      for(circle in _circles.iterator())
      {
	if(circle.model.getType() == "Shot")
	  {TotalShots++;}
      }
    if(Difficulty  >= 1)
    {
      _moneyCollected.text = "Money Collected: $" + _c1.money;
    }
    _shotsOnScreen.text =  "Shots on Screen: " + TotalShots;
    _framesLasted.text =   "Number of Frames:" + numOfFrames;
    if(Difficulty  >= 1)
    {
      TotalMoney =  Math.round(( (_c1.money+(TotalShots/100)) *MoneyMultiplier)*(1 + (numOfFrames/1000)));
      _totalAmount.text ="$" + (_c1.money*MoneyMultiplier) + " + $" + (TotalShots/100)*MoneyMultiplier + " * " + (1 + (numOfFrames/1000)) + " = $"
          + TotalMoney;
    }
    

    addChild(_finishScreen);
    State = "EndScreen";
    }
    //Unload
    //UnLoad();
  }
  
  
  //
  public function CheckForBoss()
  {

    if(_enemyCircles.first() != null)
      {return false;}
    if(_c1.invincible > 0)
      {return false;}
    if(BossController != null)
      {return false;}

    return true;

  }
  
  private function OnEnter(event:Event)
  { 
    if(State == "Playing")
    {
      if(Pause == "Paused")
        {return;}
      
    }
      if(!_c1.alive){LevelOver();return;}
      if(State == "Won"){UnLoad();return;}
      numOfFrames++;
//       if(numOfFrames  == 700)
// 	{addBoss();}
     _statusBox.text = Std.string(numOfFrames); // Std.string(flash.system.System.totalMemory);
    //_beforeTime = flash.Lib.getTimer();
    //_overSleepTime = (_beforeTime - _afterTime) - _sleepTime;
    //Update the circle
//     _c1.radius = _c1.radius - 0.1;
//     removeChild(_c1_View);
//     _c1_View = new CircleView(_c1);
//     addChild(_c1_View);
//     if(Std.random(500) == 499)
//       {
//       var random =  randomCirclePosition();
//       addHealth(Math.ceil( _c1.xPos + (_c1.radius + _c2.radius + 250)* Math.cos(random)), 
// 		 Math.ceil(_c1.yPos + (_c1.radius + _c2.radius + 150) * Math.sin(random)));
//       }

    if(Difficulty > -1)
    {
      if(numOfFrames <= StartBoss || Difficulty == 0 )
      {
      var appearance = Std.int(numOfFrames / 100);
      if(Std.random(10) <= Math.min(appearance,2))
	{addEnemy();}
      if(Std.random(100) <= Math.min(appearance-1,8))
	{addShootingEnemy();}
      if(Std.random(100) <= Math.min(Std.int(appearance/10)-1,3))
	{addInvincibleEnemy();}
      if(Std.random(400) <= Math.min(Std.int(appearance/25)-1,1))
	{addEnemyShootingEnemy();}
      }
      else
      {
	//Wait Until ready
	if(CheckForBoss())
	{
	  //Wait 10 Secs.(or so)
	  waitedFrames++;
	  if(waitedFrames > 200)
	  {
	  //addBoss()
	  addBoss();
	  }
	}
	else
	{
	if(BossController != null && BossController.subModels.first() == null)
	  {
	    BossDefeated++;
	    BossController = null;
	    if(BossDefeated  >= Difficulty)
	      {State = "Won";dispatchEvent(new Event("SceneWon"));}
	  }
	}
      }
    }
    _c1.update();
    if(_c1.xPos > stage.stageWidth + (_c1.width* 0.5))
    {
      _c1.setX = 0 - (_c1.width* 0.5);
    }
    else if (_c1.xPos < 0 - (_c1.width* 0.5))
    {
      _c1.setX = stage.stageWidth + (_c1.width* 0.5);
    }
    else if(_c1.yPos > stage.stageHeight + (_c1.height* 0.5))
    {
      _c1.setY = 0 - (_c1.height* 0.5);
    }
    else if (_c1.yPos < 0 - (_c1.height* 0.5))
    {
      _c1.setY = stage.stageHeight + (_c1.height* 0.5);
    }
    if(_c1.firing > 0)
      {
      for(i in 0...ShotsFiredRate)
	{addShot();}
      }
    
    //Update the particle
    _collision = false;
    for(circle in _circles.iterator())
    {
      circle.model.update();
      if(circle.model.xPos > stage.stageWidth + (circle.model.width* 0.5))
      {
	circle.model.setX = 0 - (circle.model.width* 0.5);
      }
      else if (circle.model.xPos < 0 - (circle.model.width* 0.5))
      {
	circle.model.setX = stage.stageWidth + (circle.model.width* 0.5);
      }
      else if(circle.model.yPos > stage.stageHeight + (circle.model.height* 0.5))
      {
	circle.model.setY = 0 - (circle.model.height* 0.5);
      }
      else if (circle.model.yPos < 0 - (circle.model.height* 0.5))
      {
	circle.model.setY = stage.stageHeight + (circle.model.height* 0.5);
      }  

  //Vector between circle and particle
      var m = Math.sqrt(Math.pow(_c1.xPos -circle.model.xPos,2) +
	    Math.pow( _c1.yPos - circle.model.yPos,2)
	  );   
      var enemyhit = false;
      if(circle.model.getType() == "Shot")
      {
	if(BossController != null)
	{
	for(enemyCircle in BossController.subModels.iterator())
	{
	    var mEnemyCircle = Math.sqrt(Math.pow(enemyCircle.model.xPos -circle.model.xPos,2) +
	      Math.pow( enemyCircle.model.yPos - circle.model.yPos,2)
	    ); 
	  var enemyCollisionTotalRadii = enemyCircle.model.radius + circle.model.radius;
	  if(mEnemyCircle < enemyCollisionTotalRadii)
	    {
		  //A collision is happening
	      removeChild(circle.view);
	      _circles.remove(circle);
	      circle = null;
	      removeChild(enemyCircle.view);
	      BossController.subModels.remove(enemyCircle);
	      BossController.CenterModel = null;
	      enemyCircle = null;
	      enemyhit = true;
	      break;
	    }
	}
	}
	for(enemyCircle in _enemyCircles.iterator())
	{
	  if (enemyCircle.model.invincible > 0)
	     {
		continue;
	     }
	  var mEnemyCircle = Math.sqrt(Math.pow(enemyCircle.model.xPos -circle.model.xPos,2) +
	      Math.pow( enemyCircle.model.yPos - circle.model.yPos,2)
	    );
	  var enemyCollisionTotalRadii = enemyCircle.model.radius + circle.model.radius;
	  if(mEnemyCircle < enemyCollisionTotalRadii)
	    {
	      var LootRandom = Std.random(100);
	      if(Difficulty <= 0){LootRandom = -1;}
	      var numAdded = 0;
	      if(LootRandom >= 0 && LootRandom < 10+(2*PowerUps))
	      {
		addHealth(enemyCircle.model.xPos-(2*numAdded),enemyCircle.model.yPos);
		numAdded++;
	      }
	      if(LootRandom >= 20-(2*PowerUps) && LootRandom <= 80+(3*PowerUps))
	      {
		addMoney(enemyCircle.model.xPos-(2*numAdded),enemyCircle.model.yPos);
		numAdded++;
	      }
	      if(Difficulty < 3)
	      {
		var Appear = 99;
		if(Difficulty <= 1)
		{
		  Appear = Std.int(99 - (PowerUps/2));
		}
		if(LootRandom >= Appear)
		{
		  addInvincibility(enemyCircle.model.xPos-(2*numAdded),enemyCircle.model.yPos);
		  numAdded++;
		}
	      }
		  //A collision is happening
	      removeChild(circle.view);
	      _circles.remove(circle);
	      circle = null;
	      removeChild(enemyCircle.view);
	      _enemyCircles.remove(enemyCircle);
	      enemyCircle = null;
	      enemyhit = true;
	      break;
	    }
	}
      }
      if(enemyhit)
	{break;}
      //Calculate the radii of both circles combined
      var totalRadii = _c1.radius + circle.model.radius; 
      if(m < totalRadii)
      {
	if(circle.model.getType() == "Health")
	   {_c1.heal();}
	else if(circle.model.getType() == "Money")
	   {_c1.money += 1;}
	else if(circle.model.getType() == "Invincibility")
	   {_c1.invincible = 1;}
	else
	  {_c1.hit();}
	//A collision is happening
	removeChild(circle.view);
	_circles.remove(circle);
	circle = null;
	_collision = true;
	
      }
      if(circle.model.Alpha == 0)
	{
	//A collision is happening
	removeChild(circle.view);
	circle.model = null;
	_circles.remove(circle);
	circle = null;
	}
    }
    
    if(BossController != null)
    {
    for(enemyCircle in BossController.subModels.iterator())
    {

      if(enemyCircle.model.getType() == "InvincibleEnemy")
      {
	if(Std.random(1000) == 9)
	{
	 enemyCircle.model.invincible = 1;
	}
      }
      if(enemyCircle.model.getType() == "EnemyShootingEnemy")
      {
	if(Std.random(100) == 9)
	{
	  _c2 = new CircleModel("Enemy",3,0xFF0000);
	  _EnemyController = new EnemyShipController(_c2,_c1);
	  _c2_View = new CircleView(_c2);

	  addChild(_c2_View);
	  var y1 = _c1.yPos;
	  var y2 = enemyCircle.model.yPos;
	  var x1 = _c1.xPos;
	  var x2 = enemyCircle.model.xPos;
	  var y =(y2- y1);
	  var x = (x2 - x1);
	  var m = y/x;
	  var angle = Math.atan2(y,x);
	  //trace(angle);
	  //angle = angle / (2*Math.PI);
	  //trace(angle);
	 // trace("y " + y + " x " + x + " Angle " + angle + " cos " +   Math.cos(angle) + " Sin  " + Math.sin(angle) );
	  _c2.vx = 2*-Math.cos(angle);
	  _c2.vy = 2*-Math.sin(angle);

	  _c2.setX = enemyCircle.model.xPos + (3 *_c2.vx);
	  _c2.setY = enemyCircle.model.yPos + (3 *_c2.vy);
	  _c2.friction = .999;
	  _enemyCircles.add(new CircleMV(_c2,_c2_View));
	  _c2 = null;
	  _c2_View = null;
	}
      }
      if(enemyCircle.model.getType() == "ShootingEnemy")
      {
	if(Std.random(100) == 9)
	{
	  _c2 = new CircleModel("Shot",3);
	  _c2_View = new CircleView(_c2);

	  addChild(_c2_View);
	  var y1 = _c1.yPos;
	  var y2 = enemyCircle.model.yPos;
	  var x1 = _c1.xPos;
	  var x2 = enemyCircle.model.xPos;
	  var y =(y2- y1);
	  var x = (x2 - x1);
	  var m = y/x;
	  var angle = Math.atan2(y,x);
	  //trace(angle);
	  //angle = angle / (2*Math.PI);
	  //trace(angle);
	 // trace("y " + y + " x " + x + " Angle " + angle + " cos " +   Math.cos(angle) + " Sin  " + Math.sin(angle) );
	  _c2.vx = 2*-Math.cos(angle);
	  _c2.vy = 2*-Math.sin(angle);

	  _c2.setX = enemyCircle.model.xPos + (3 *_c2.vx);
	  _c2.setY = enemyCircle.model.yPos + (3 *_c2.vy);
	  _c2.friction = .999;
	  _circles.add(new CircleMV(_c2,_c2_View));
	  _c2 = null;
	  _c2_View = null;
	}
      }
      enemyCircle.model.update();
      if(enemyCircle.model.xPos > stage.stageWidth + (enemyCircle.model.width* 0.5))
      {
	enemyCircle.model.setX = 0 - (enemyCircle.model.width* 0.5);
      }
      else if (enemyCircle.model.xPos < 0 - (enemyCircle.model.width* 0.5))
      {
	enemyCircle.model.setX = stage.stageWidth + (enemyCircle.model.width* 0.5);
      }
      else if(enemyCircle.model.yPos > stage.stageHeight + (enemyCircle.model.height* 0.5))
      {
	enemyCircle.model.setY = 0 - (enemyCircle.model.height* 0.5);
      }
      else if (enemyCircle.model.yPos < 0 - (enemyCircle.model.height* 0.5))
      {
	enemyCircle.model.setY = stage.stageHeight + (enemyCircle.model.height* 0.5);
      }  

  //Vector between circle and particle
      var m = Math.sqrt(Math.pow(_c1.xPos -enemyCircle.model.xPos,2) +
	    Math.pow( _c1.yPos - enemyCircle.model.yPos,2)
	  );   
      
      //Calculate the radii of both circles combined
      var totalRadii = _c1.radius + enemyCircle.model.radius; 
      if(m < totalRadii)
      {
	_c1.hit();
	//A collision is happening
	
	removeChild(enemyCircle.view);
	BossController.subModels.remove(enemyCircle);
	BossController.CenterModel = null;
	enemyCircle = null;
	_collision = true;
	
      }
    }
    }

    //Update Enemy
    for(enemyCircle in _enemyCircles.iterator())
    {
      if(enemyCircle.model.getType() == "InvincibleEnemy")
      {
	if(Std.random(1000) == 9)
	{
	 enemyCircle.model.invincible = 1;
	}
      }
      if(enemyCircle.model.getType() == "EnemyShootingEnemy")
      {
	if(Std.random(100) == 9)
	{
	  _c2 = new CircleModel("Enemy",3,0xFF0000);
	  _EnemyController = new EnemyShipController(_c2,_c1);
	  _c2_View = new CircleView(_c2);

	  addChild(_c2_View);
	  var y1 = _c1.yPos;
	  var y2 = enemyCircle.model.yPos;
	  var x1 = _c1.xPos;
	  var x2 = enemyCircle.model.xPos;
	  var y =(y2- y1);
	  var x = (x2 - x1);
	  var m = y/x;
	  var angle = Math.atan2(y,x);
	  //trace(angle);
	  //angle = angle / (2*Math.PI);
	  //trace(angle);
	 // trace("y " + y + " x " + x + " Angle " + angle + " cos " +   Math.cos(angle) + " Sin  " + Math.sin(angle) );
	  _c2.vx = 2*-Math.cos(angle);
	  _c2.vy = 2*-Math.sin(angle);

	  _c2.setX = enemyCircle.model.xPos + (3 *_c2.vx);
	  _c2.setY = enemyCircle.model.yPos + (3 *_c2.vy);
	  _c2.friction = .999;
	  _enemyCircles.add(new CircleMV(_c2,_c2_View));
	  _c2 = null;
	  _c2_View = null;
	}
      }
      if(enemyCircle.model.getType() == "ShootingEnemy")
      {
	if(Std.random(100) == 9)
	{
	  _c2 = new CircleModel("Shot",3);
	  _c2_View = new CircleView(_c2);

	  addChild(_c2_View);
	  var y1 = _c1.yPos;
	  var y2 = enemyCircle.model.yPos;
	  var x1 = _c1.xPos;
	  var x2 = enemyCircle.model.xPos;
	  var y =(y2- y1);
	  var x = (x2 - x1);
	  var m = y/x;
	  var angle = Math.atan2(y,x);
	  //trace(angle);
	  //angle = angle / (2*Math.PI);
	  //trace(angle);
	 // trace("y " + y + " x " + x + " Angle " + angle + " cos " +   Math.cos(angle) + " Sin  " + Math.sin(angle) );
	  _c2.vx = 2*-Math.cos(angle);
	  _c2.vy = 2*-Math.sin(angle);

	  _c2.setX = enemyCircle.model.xPos + (3 *_c2.vx);
	  _c2.setY = enemyCircle.model.yPos + (3 *_c2.vy);
	  _c2.friction = .999;
	  _circles.add(new CircleMV(_c2,_c2_View));
	  _c2 = null;
	  _c2_View = null;
	}
      }
      enemyCircle.model.update();
      if(enemyCircle.model.xPos > stage.stageWidth + (enemyCircle.model.width* 0.5))
      {
	enemyCircle.model.setX = 0 - (enemyCircle.model.width* 0.5);
      }
      else if (enemyCircle.model.xPos < 0 - (enemyCircle.model.width* 0.5))
      {
	enemyCircle.model.setX = stage.stageWidth + (enemyCircle.model.width* 0.5);
      }
      else if(enemyCircle.model.yPos > stage.stageHeight + (enemyCircle.model.height* 0.5))
      {
	enemyCircle.model.setY = 0 - (enemyCircle.model.height* 0.5);
      }
      else if (enemyCircle.model.yPos < 0 - (enemyCircle.model.height* 0.5))
      {
	enemyCircle.model.setY = stage.stageHeight + (enemyCircle.model.height* 0.5);
      }  

  //Vector between circle and particle
      var m = Math.sqrt(Math.pow(_c1.xPos -enemyCircle.model.xPos,2) +
	    Math.pow( _c1.yPos - enemyCircle.model.yPos,2)
	  );   
      
      //Calculate the radii of both circles combined
      var totalRadii = _c1.radius + enemyCircle.model.radius; 
      if(m < totalRadii)
      {
	_c1.hit();
	//A collision is happening
	removeChild(enemyCircle.view);
	_enemyCircles.remove(enemyCircle);
	enemyCircle = null;
	_collision = true;
	
      }
    }
    //Update status box
//            _afterTime = flash.Lib.getTimer();
//        _timeDiff = _afterTime - _beforeTime;
//        _sleepTime = (Std.int(_period) - _timeDiff) - _overSleepTime;
//       if(_sleepTime <= 0) {
// 	_excess -= Std.int(_sleepTime);
// 	_sleepTime = 2;
//       }
//       myTimer.reset();
//       myTimer.delay = _sleepTime;
//       myTimer.start();
//       event.updateAfterEvent;
  }

}